home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Graphics / Utilities / CFG 2.1 UR / Convert Earlier Than 1.2 / convert file.c next >
C/C++ Source or Header  |  1990-10-08  |  11KB  |  458 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "OSUtil.h"
  4. #include "StdFilePkg.h"
  5. #include "FileMgr.h"
  6.  
  7.  
  8. typedef struct JS_FRACTAL_120
  9. {
  10.     float        version;
  11.     int            function;
  12.     long        accuracy;
  13.     int            freedom;
  14.     double        centerX;
  15.     double         centerY;
  16.     double        scale;
  17.     double        fixedRe;
  18.     double         fixedIm;
  19.     Boolean        invertKolor;
  20.     Boolean        limitExtreme;
  21.     long        maxKolors;
  22.     int            maxPixels;
  23.     long        iterations;
  24.     Point        fractalSize;    /* set to equal window size when drawn */
  25.     Point        windowSize;        /* set for fractal size; do not change fract size since still need it for zooming */
  26.     Point        actualSize;        /* actual size of fractal drawn */
  27. } JS_fractal_120;
  28.  
  29.  
  30. #define TRUE                     1
  31. #define FALSE                     0
  32. #define REMOVE_ALL_EVENTS         -1
  33. #define NIL_POINTER                0L
  34. #define NIL_STRING                "\p"
  35. #define NAME_STRING                "untitled"
  36. #define IGNORED_STRING            NIL_STRING
  37. #define    NIL_FILE_FILTER            NIL_POINTER
  38. #define NIL_DIALOG_HOOK            NIL_POINTER
  39. #define MOVE_TO_FRONT            -1
  40. #define DEFAULT_BUTTON            1
  41. #define VERSION_DIALOG            400
  42. #define VERSION_OK                1
  43. #define VERSION_CANCEL            2
  44. #define TOL                        1.0e-5
  45. #define VERSION                    1.2
  46. #define SYS_VERSION                2
  47.  
  48.  
  49. JS_fractal_120 gFractal;
  50.  
  51.  
  52. /*
  53.     FILES HAS BEEN EXTENSIVELY REWORKED (AGAIN).
  54.     
  55.     This converts files from version 1.0x and 1.1x to version 1.2.  This version
  56.     includes data on the version (so future versions only need to know the structure
  57.     number to read in data), type of file saved (currently 0 = preferences, 
  58.     1 = mandelbrot, 2 = julia, and 3 = random walk; 0 and 3 are not written yet).
  59.     Information is also present on the fractal screen size and actual fractal
  60.     size).
  61.     
  62.     This program simply shows a standard file dialog to get the file to convert.
  63.     If a file is "opened" or double-clicked, it will be converted and overwrite
  64.     the previous file.  The dialog will then be shown again.  Clicking on cancel quits
  65.     the program.  Please note, this program will destroy data if version 1.2 is converted
  66.     to 1.2 (since both have the same creator and file type, version 1.2 files will be
  67.     displayed).  This program assumes that those files that are double clicked
  68.     or opened are from version 1.0x or 1.1x.
  69. */
  70.  
  71.  
  72. void main(void);
  73. void ToolBoxInit(void);
  74. void GetFileName( SFReply * );
  75. void OpenBinaryFile( char * );
  76. void ReadFileData( SFReply * );
  77. void CurrDeviceSize( int *, int * );
  78. void WriteFileData( SFReply * );
  79. void DoVersionUpdate(void);
  80. char HandleMightBeCurrVersion(void);
  81. void centerWindow(WindowPtr);
  82. void buttonProc (DialogPtr, int);
  83. int  NoSysError(void);
  84.  
  85. /* --------------------------------------------------------------------------------- */
  86.  
  87.  
  88. void main(void)
  89. {
  90.     char op = TRUE;
  91.     
  92.     ToolBoxInit();
  93.     if (NoSysError())
  94.     {
  95.         while (op == TRUE)
  96.         {
  97.             OpenBinaryFile(&op);
  98.         }
  99.     }
  100.     ExitToShell();
  101. }
  102.  
  103.  
  104. /* --------------------------------------------------------------------------------- */
  105.  
  106.  
  107. void ToolBoxInit(void)
  108. {
  109.     InitGraf( &thePort );
  110.     InitFonts();
  111.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  112.     InitWindows();
  113.     InitMenus();
  114.     TEInit();
  115.     InitDialogs( NIL_POINTER );
  116.     InitCursor();
  117. }
  118.  
  119.  
  120. /* --------------------------------------------------------------------------------- */
  121.  
  122.  
  123. void GetFileName( replyPtr )
  124.  
  125. SFReply *replyPtr;
  126.  
  127. {
  128.     Point thePoint;
  129.     SFTypeList    typeList;
  130.     int numTypes;
  131.     
  132.     thePoint.h = 100;
  133.     thePoint.v = 100;
  134.     typeList[0] = 'fraJ';
  135.     numTypes = 1;
  136.     SFGetFile( thePoint, IGNORED_STRING, NIL_FILE_FILTER, numTypes, 
  137.             &typeList, NIL_DIALOG_HOOK, replyPtr );
  138. }
  139.  
  140.  
  141. /* --------------------------------------------------------------------------------- */
  142.  
  143.  
  144. void OpenBinaryFile( opened )
  145.  
  146. char *opened;
  147.  
  148. {
  149.     SFReply    reply;
  150.  
  151.     GetFileName( &reply );
  152.     if (reply.good)
  153.     {
  154.         ReadFileData( &reply );
  155.         DoVersionUpdate();
  156.         WriteFileData( &reply );
  157.     }
  158.     else
  159.     {
  160.         *opened = FALSE;
  161.     }
  162. }
  163.  
  164.  
  165. /* --------------------------------------------------------------------------------- */
  166.  
  167.  
  168. void ReadFileData( replyPtr )
  169.  
  170. SFReply *replyPtr;
  171.  
  172. {
  173.     int     srcFile;
  174.     OSErr     theErr;
  175.     long     siz;
  176.     int        tempi;
  177.     long     templ;
  178.     float    tempf;
  179.     Boolean    tempB;
  180.     char    c = FALSE;
  181.  
  182.     theErr = FSOpen( replyPtr->fName, replyPtr->vRefNum, &srcFile );
  183.     if (theErr == noErr)
  184.     {
  185.         SetFPos( srcFile, fsFromStart, 0L );
  186.         siz = (long) sizeof(float);
  187.         FSRead( srcFile, &siz, &tempf );
  188.         if ((fabs(tempf - TOL) < VERSION) && (fabs(tempf + TOL) > VERSION))
  189.         {
  190.             c = HandleMightBeCurrVersion();
  191.         }
  192.         if (c == TRUE)
  193.         {
  194.             FSClose( srcFile );
  195.             return;
  196.         }
  197.         SetFPos( srcFile, fsFromStart, 0L );
  198.         siz = (long) sizeof(int);
  199.         FSRead( srcFile, &siz, &(gFractal.function) );
  200.         siz = (long) sizeof(int);
  201.         FSRead( srcFile, &siz, &tempi );
  202.         siz = (long) sizeof(int);
  203.         FSRead( srcFile, &siz, &(gFractal.freedom) );
  204.         siz = (long) sizeof(double);
  205.         FSRead( srcFile, &siz, &(gFractal.centerX) );
  206.         siz = (long) sizeof(double);
  207.         FSRead( srcFile, &siz, &(gFractal.centerY) );
  208.         siz = (long) sizeof(double);
  209.         FSRead( srcFile, &siz, &(gFractal.scale) );
  210.         siz = (long) sizeof(double);
  211.         FSRead( srcFile, &siz, &(gFractal.fixedRe) );
  212.         siz = (long) sizeof(double);
  213.         FSRead( srcFile, &siz, &(gFractal.fixedIm) );
  214.         siz = (long) sizeof(Boolean);
  215.         FSRead( srcFile, &siz, &(gFractal.invertKolor) );
  216.         siz = (long) sizeof(Boolean);
  217.         FSRead( srcFile, &siz, &(gFractal.limitExtreme) );
  218.         siz = (long) sizeof(long);
  219.         FSRead( srcFile, &siz, &(gFractal.maxKolors) );
  220.         siz = (long) sizeof(Boolean);
  221.         FSRead( srcFile, &siz, &tempB );
  222.         siz = (long) sizeof(int);
  223.         FSRead( srcFile, &siz, &(gFractal.maxPixels) );
  224.         siz = (long) sizeof(long);
  225.         theErr = FSRead( srcFile, &siz, &templ );
  226.         if (theErr == noErr)
  227.         {
  228.             gFractal.accuracy = templ;
  229.         }
  230.         else
  231.         {
  232.             gFractal.accuracy = (long) tempi;
  233.         }
  234.         FSClose( srcFile );
  235.     }
  236. }
  237.  
  238.  
  239. /* --------------------------------------------------------------------------------- */
  240.  
  241.  
  242. void CurrDeviceSize( x, y )
  243.  
  244. /* This returns the max screen size divided by 2 */
  245.  
  246. int *x;
  247. int *y;
  248.  
  249. {
  250.     *x = (screenBits.bounds.right) / 2;
  251.     *y = (screenBits.bounds.bottom) / 2;
  252. }
  253.  
  254.  
  255. /* --------------------------------------------------------------------------------- */
  256.  
  257.  
  258. void WriteFileData( replyPtr )
  259.  
  260. SFReply *replyPtr;
  261.  
  262. {
  263.     int     srcFile;
  264.     OSErr     theErr;
  265.     Str255     k;
  266.     long     siz;
  267.     int     i;
  268.     
  269.     theErr = Create( replyPtr->fName, replyPtr->vRefNum, 'fraG', 'fraJ');
  270.     if ((theErr == noErr) || (theErr == dupFNErr))
  271.     {
  272.         theErr = FSOpen( replyPtr->fName, replyPtr->vRefNum, &srcFile );
  273.         if (theErr == noErr)
  274.         {
  275.             SetFPos( srcFile, fsFromStart, 0L );
  276.             siz = (long) sizeof(float);
  277.             FSWrite( srcFile, &siz, &(gFractal.version) );
  278.             siz = (long) sizeof(int);
  279.             FSWrite( srcFile, &siz, &(gFractal.function) );
  280.             siz = (long) sizeof(long);
  281.             FSWrite( srcFile, &siz, &(gFractal.accuracy) );
  282.             siz = (long) sizeof(int);
  283.             FSWrite( srcFile, &siz, &(gFractal.freedom) );
  284.             siz = (long) sizeof(double);
  285.             FSWrite( srcFile, &siz, &(gFractal.centerX) );
  286.             siz = (long) sizeof(double);
  287.             FSWrite( srcFile, &siz, &(gFractal.centerY) );
  288.             siz = (long) sizeof(double);
  289.             FSWrite( srcFile, &siz, &(gFractal.scale) );
  290.             siz = (long) sizeof(double);
  291.             FSWrite( srcFile, &siz, &(gFractal.fixedRe) );
  292.             siz = (long) sizeof(double);
  293.             FSWrite( srcFile, &siz, &(gFractal.fixedIm) );
  294.             siz = (long) sizeof(Boolean);
  295.             FSWrite( srcFile, &siz, &(gFractal.invertKolor) );
  296.             siz = (long) sizeof(Boolean);
  297.             FSWrite( srcFile, &siz, &(gFractal.limitExtreme) );
  298.             siz = (long) sizeof(long);
  299.             FSWrite( srcFile, &siz, &(gFractal.maxKolors) );
  300.             siz = (long) sizeof(int);
  301.             FSWrite( srcFile, &siz, &(gFractal.maxPixels) );
  302.             siz = (long) sizeof(long);
  303.             FSWrite( srcFile, &siz, &(gFractal.iterations) );
  304.             siz = (long) sizeof(int);
  305.             FSWrite( srcFile, &siz, &(gFractal.fractalSize.h) );
  306.             siz = (long) sizeof(int);
  307.             FSWrite( srcFile, &siz, &(gFractal.fractalSize.v) );
  308.             siz = (long) sizeof(int);
  309.             FSWrite( srcFile, &siz, &(gFractal.windowSize.h) );
  310.             siz = (long) sizeof(int);
  311.             FSWrite( srcFile, &siz, &(gFractal.windowSize.v) );
  312.             siz = (long) sizeof(int);
  313.             FSWrite( srcFile, &siz, &(gFractal.actualSize.h) );
  314.             siz = (long) sizeof(int);
  315.             FSWrite( srcFile, &siz, &(gFractal.actualSize.v) );
  316.  
  317.             GetFPos( srcFile, &siz );
  318.             SetEOF( srcFile, siz );
  319.             FSClose( srcFile );
  320.             FlushVol(0L, replyPtr->vRefNum);
  321.         }
  322.     }
  323. }
  324.  
  325.  
  326. /* --------------------------------------------------------------------------------- */
  327.  
  328.  
  329. void DoVersionUpdate(void)
  330. {
  331.     int x;
  332.     int y;
  333.     
  334.     gFractal.version = VERSION;
  335.     CurrDeviceSize(&x,&y);
  336.     gFractal.windowSize.h = x * 2;
  337.     gFractal.windowSize.v = y * 2;
  338.     gFractal.fractalSize.h = 0;
  339.     gFractal.fractalSize.v = 0;
  340.     gFractal.actualSize.h = 0;
  341.     gFractal.actualSize.v = 0;
  342. }
  343.  
  344.  
  345. /* --------------------------------------------------------------------------------- */
  346.  
  347.  
  348. char HandleMightBeCurrVersion(void)
  349. {
  350.     Boolean        dialogDone = FALSE;
  351.     int         itemHit;
  352.     int            itemType;
  353.     Rect        itemRect;
  354.     Handle        itemHandle;
  355.     char         c;
  356.     DialogPtr    gSettingsDialog;
  357.     
  358.     gSettingsDialog = GetNewDialog( VERSION_DIALOG, NIL_POINTER, MOVE_TO_FRONT );
  359.     centerWindow( gSettingsDialog );
  360.     ShowWindow( gSettingsDialog );
  361.     buttonProc( gSettingsDialog, DEFAULT_BUTTON );
  362.     
  363.     while (dialogDone == FALSE)
  364.     {
  365.         ModalDialog( NIL_POINTER, &itemHit );
  366.         switch( itemHit )
  367.         {
  368.             case VERSION_OK:
  369.                     c = FALSE;
  370.                     dialogDone = TRUE;
  371.                     break;
  372.             case VERSION_CANCEL:
  373.                     c = TRUE;
  374.                     dialogDone = TRUE;
  375.                     break;
  376.             default:
  377.                     break;
  378.         }
  379.     }
  380.     HideWindow( gSettingsDialog );
  381.     DisposDialog( gSettingsDialog );
  382.     return c;
  383. }
  384.  
  385.  
  386. /* --------------------------------------------------------------------------------- */
  387.  
  388.  
  389. void centerWindow(theWindow)
  390.  
  391. WindowPtr        theWindow;
  392.  
  393. {
  394.     short            scrLength, scrHeight;
  395.     Rect            globalRect;
  396.     Point            where;
  397.  
  398.     /* get the window rect in global coordinates */
  399.     CurrDeviceSize( &scrLength, &scrHeight );
  400.     globalRect = (*theWindow).portRect;
  401.     
  402.     scrLength *= 2;
  403.     scrHeight *= 2;
  404.     where.v = (scrHeight - (globalRect.bottom - globalRect.top)) / 2;
  405.     where.h = (scrLength - (globalRect.right - globalRect.left)) / 2 + GetMBarHeight();
  406.  
  407.     MoveWindow (theWindow, where.h, where.v, FALSE);
  408.     
  409. } /* centerWindow */
  410.  
  411.  
  412. /* --------------------------------------------------------------------------------- */
  413.  
  414.  
  415. void buttonProc (theDialog, theItem)
  416.  
  417. DialogPtr        theDialog;
  418. int                theItem;
  419.  
  420. {
  421.     short            type;
  422.     Rect            box;
  423.     Handle            itemHdl;
  424.     GrafPtr            oldGrafPtr;
  425.     
  426.     GetPort( &oldGrafPtr );
  427.     SetPort(theDialog);
  428.     GetDItem (theDialog, theItem, &type, &itemHdl, &box);
  429.     PenSize (3, 3);
  430.     InsetRect (&box, -4, -4);
  431.     FrameRoundRect (&box, 16, 16);
  432.     PenNormal ();
  433.     SetPort( oldGrafPtr );
  434.     
  435. } /* buttonProc */
  436.  
  437.  
  438. /* --------------------------------------------------------------------------------- */
  439.  
  440.  
  441. int NoSysError(void)
  442. {
  443.     OSErr status;
  444.     SysEnvRec SysEnvData;
  445.     
  446.     status = SysEnvirons( SYS_VERSION, &SysEnvData );
  447.     if (( status != noErr ) || ( SysEnvData.processor < env68020 ) || 
  448.         ( SysEnvData.systemVersion < 0x0600 ) || ( SysEnvData.hasFPU != TRUE ) ||
  449.         ( SysEnvData.hasColorQD != TRUE ))
  450.     {
  451.         return(FALSE);
  452.     }
  453.     else
  454.     {
  455.         return(TRUE);
  456.     }
  457. }
  458.